package edu.bu.manateem.msg.heatmap;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGDocument;

public class HeatmapScale 
{
	private SVGDocument parentDocument;
	private int imageHeight;
	private int interval;
	private double bin;

	public HeatmapScale(SVGDocument parentDocument, int imageHeight, int interval, double bin) 
	{
		this.parentDocument = parentDocument;
		this.imageHeight = imageHeight;
		this.interval = interval;
		this.bin = bin;
	}

	public List<Element> getTicks()
	{
		List<Element> ticks = new ArrayList<Element>();
		
		final String line = "line";
		final String style = "style";
		final String styleSetting = "stroke:rgb(0, 0, 255); stroke-width:2";
		Set<Integer> includedLevels = new HashSet<Integer>();
		for (int i = 0; i < this.imageHeight; i++)
		{
			Integer level =  (int) (i * this.bin);
			
			if ( level % this.interval == 0 && !includedLevels.contains(level))
			{
				Element vLine = parentDocument.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, line);
				vLine.setAttributeNS(null, style, styleSetting);
				
				vLine.setAttributeNS(null, "x1", "-1");
				vLine.setAttributeNS(null, "y1", Integer.toString(this.imageHeight - i));
				
				vLine.setAttributeNS(null, "x2", "10");
				vLine.setAttributeNS(null, "y2", Integer.toString(this.imageHeight - i));
				
				ticks.add(vLine);
				i += this.interval;
				includedLevels.add(level);
			}
		}
		return ticks;
	}
	
	public List<Element> getLabels()
	{
		List<Element> labels = new ArrayList<Element>();
		
		final String text = "text";
		final String style = "style";
		final String styleSetting = "font-family:Verdana;font-size:18";
		Set<Integer> includedLevels = new HashSet<Integer>();
		
		for (int i = 0; i < this.imageHeight; i++)
		{
			Integer level =  (int) (i * this.bin);
			if (  level % this.interval == 0 && !includedLevels.contains(level))
			{
				Element textLabel = parentDocument.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, text);
				
				textLabel.setAttributeNS(null, style, styleSetting);
				textLabel.setAttributeNS(null, "x", "20");
				textLabel.setAttributeNS(null, "y", Integer.toString(this.imageHeight - i));				
				
				textLabel.appendChild(this.parentDocument.createTextNode(Integer.toString(level)));
				
				labels.add(textLabel);
				i += this.interval;
				includedLevels.add(level);
			}
		}
		
		
		return labels;
	}

	public Element getVerticalLine()
	{
		Element vLine = parentDocument.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "line");

		vLine.setAttributeNS(null, "x1", "-1");
		vLine.setAttributeNS(null, "y1", "-1");

		vLine.setAttributeNS(null, "x2", "-1");
		vLine.setAttributeNS(null, "y2", Integer.toString(imageHeight + 1));

		vLine.setAttributeNS(null, "style", "stroke:rgb(99,99,99); stroke-width:10");
		return vLine;
	}
}
